/**
 * 项目名：化工企业安全生产信息化管理平台
 * 日期：    2020-03-17 14:00:30
 * Copyright (c) 2015- -版权所有
 */

package com.joysuch.wwyt.edu.controller;

import cn.zlg.common.springmvc.validate.constraints.NotNull;
import com.joysuch.wwyt.core.bean.SimpleSelectListBean;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.common.util.PageDataUtils;
import com.joysuch.wwyt.core.service.SimpleSelectListService;
import com.joysuch.wwyt.edu.bean.EduCertPageParam;
import com.joysuch.wwyt.edu.bean.EduCertificateEditBean;
import com.joysuch.wwyt.edu.bean.EduCertificateListBean;
import com.joysuch.wwyt.edu.bean.EduCertificateListBean.SearchEduCertificate;
import com.joysuch.wwyt.edu.entity.EduCertificate;
import com.joysuch.wwyt.edu.service.EduCertificateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/edu/certificate")
@Api(tags = "/edu/certificate 教育-证书")
public class EduCertificateController {

    private static final Logger logger = LoggerFactory.getLogger(EduCertificateController.class);

    @Autowired
    private EduCertificateService eduCertificateService;
    @Autowired
    private SimpleSelectListService simpleSelectListService;

    @RequiresPermissions("edu:certificate:add")
    @RequestMapping("add")
    @ApiOperation("教育-证书添加")
    public Object add(@Valid @RequestBody EduCertificateEditBean entity) {
        eduCertificateService.add(entity);
        return ResultBean.success("");
    }

    @RequiresPermissions("edu:certificate:update")
    @RequestMapping("update")
    public Object update(@Valid @RequestBody EduCertificateEditBean entity) {
        if (entity.getId() == null) {
            return ResultBean.fail(102, "未指定主键");
        }
        eduCertificateService.update(entity);
        return ResultBean.success("");
    }

    // @RequiresPermissions("edu:certificate")
    @RequestMapping("page")
    public Object page(Pageable page, SearchEduCertificate condition) {
        page = PageDataUtils.addOrderByDesc(page, "id");
        Page<EduCertificateListBean> data = eduCertificateService.findByPage(page, condition);
        return ResultBean.pageData(data.getContent(), data.getTotalElements());
    }

    // @RequiresPermissions("edu:certificate")
    @RequestMapping("select-page")
    public Object selectPage(Pageable page, SearchEduCertificate condition) {
        page = PageDataUtils.addOrderByDesc(page, "id");
        Page<EduCertificateListBean> data = eduCertificateService.findByPage(page, condition);
        return ResultBean.pageData(data.getContent(), data.getTotalElements());
    }

    @RequestMapping("simple-list")
    public Object simpleList(Integer useState) {
//        List<SimpleSelectListBean> data = simpleSelectListService.list(EduCertificate.class, "name");
        List<SimpleSelectListBean> data = eduCertificateService.getSimpleList(useState);
        return ResultBean.success(data);
    }

    @GetMapping("simple-page")
    @ApiOperation("证书选择优化")
    public ResultBean simplePage(Pageable pageable, EduCertPageParam param) {
        return eduCertificateService.getSimplePage(pageable, param);
    }

    @RequiresPermissions("edu:certificate:delete")
    @RequestMapping("delete")
    public Object delete(Long id) throws Exception {
        eduCertificateService.delete(id);
        return ResultBean.success("");
    }

    // @RequiresPermissions("edu:certificate")
    @RequestMapping("get")
    public Object getById(@Valid @NotNull(field = "id") Long id) {
        return ResultBean.success(eduCertificateService.getById(id));
    }

}
